﻿
$.ajaxSetup({ url: "Proxy.ashx", type: "post", dataType: "json" });

// 绑定部门列表
function BindDepartment() {
    $.ajax({
        async: false,
        data: { op: "ListDept" }
        , success: function (json) {
            $("#ddl_dept").append("#EditUser_Dept", json.ListDept);
            DepartmentSelected();
        }
    });
}

// 部门列表选择时绑定岗位列表
function DepartmentSelected() {
    var dept_id = $("#ddl_dept ").val();
    $.ajax({
        async: false,
        data: { op: "ListJobByDept", dept_id: dept_id }
        , success: function (json) {            
            if ($("#ddl_job option").length > 0) {
                $("#ddl_job").empty();
            }
            $("#ddl_job").append("#EditUser_Job", json.ListJobByDept);
        }
    });
}

// 绑定角色列表
function BindRoles() {
    $.ajax({
        async: false,
        data: { op: "ListRole" }
        , success: function (json) {
            $("#ddl_roles").append("#EditUser_Roles", json.ListRole);
        }
    });
}

function btnEdit_Click() {
    var deptId = $("#ddl_dept").val(); // 所属机构
    var jobId = $("#ddl_job").val(); //岗位
    var roleId = $("#ddl_roles").val();  // 角色权限
    var loginName = $("#txtLoginName").val(); // 登录名
    var username = $("#txtUsername").val(); // 用户名
    var idCard = $("#txtIdCard").val(); // 身份证号
    var gender = $('input:radio[name="gender"]:checked').val(); // 性别
    var phone = $("#txtMobile").val(); // 手机
    var userPhoto = $("#txtPhoto").val(); // 照片
    var userId = QueryString("id");

    if (gender == null) {
        alert("什么也没选中!");
        return false;
    }



    $.ajax({
        data: { op: "EditUser",
            userId:userId,
            deptId: deptId,
            jobId: jobId,
            roleId: roleId,
            loginName: loginName,
            username: username,
            gender: gender,
            IDNo: idCard,
            phone: phone,
            userPhoto: userPhoto
        }
        , success: function (json) {

            var isSuccess = json.EditUser[0].status;
            if (isSuccess == "success") {
                alert("用户修改成功！")
            }
            else {
                alert("用户修改失败！详细信息：\r\n"+json.EditUser[0].falseReason);
            }
        }
    });
}

function GetUserInfoById() {
    var userid = QueryString("id");
    if (userid != null) {
        $.ajax({
            data: { op: "GetUserInfoById",
                userId: userid
            }
        , success: function (json) {

            //alert(json.UserInfo[0].job);

            $("#ddl_dept option[text='" + json.UserInfo[0].dept + "']").attr("selected", true);
            $("#ddl_job option[text='" + json.UserInfo[0].job + "']").attr("selected", true);
            $("#ddl_roles option[text='" + json.UserInfo[0].role + "']").attr("selected", true);
            
            $("#txtLoginName").val(json.UserInfo[0].loginName);
            $("#txtUsername").val(json.UserInfo[0].username);
            $("#txtIdCard").val(json.UserInfo[0].gender);
            $("input:radio[value='" + json.UserInfo[0].gender + "']").attr("checked", 'checked');
            $("#txtMobile").val(json.UserInfo[0].phone);
            $("#txtIdCard").val(json.UserInfo[0].IDNo);
            $("#txtPhoto").val(json.UserInfo[0].userPhoto);
        }
        });
    }
}

function init() {
    BindDepartment();
    BindRoles();
    GetUserInfoById();
    $("#btnEdit").href = "javascript:;";
    $("#btnCancel").href = "javascript:;";

    $("#btnEdit").click(function () {        
        btnEdit_Click();
    });

    $("#btnCancel").click(function () { 
    
     });
}
